home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1999 March / EnigmA AMIGA RUN 35 (1999)(G.R. Edizioni)(IT)[!][issue 1999-03].iso / earcd / -archivi / -recent2 / expanddict.lha / ExpandDict / original / expanddict.c next >
C/C++ Source or Header  |  1992-08-02  |  4KB  |  125 lines

  1. /****************************************************************************/
  2. /* (C) Copyright 1992 Universidade Estadual de Campinas (UNICAMP)           */
  3. /*                    Campinas, SP, Brazil                                  */
  4. /*                                                                          */
  5. /* Authors:                                                                 */
  6. /*                                                                          */
  7. /*   Tomasz Kowaltowski  - CS Dept, UNICAMP <tomasz@dcc.unicamp.ansp.br>    */
  8. /*   Claudio L. Lucchesi - CS Dept, UNICAMP <lucchesi@dcc.unicamp.ansp.br>  */
  9. /*   Jorge Stolfi        - DEC Systems Research Center <stolfi@src.dec.com> */
  10. /*                                                                          */
  11. /* This file can be freely distributed, modified, and used for any          */
  12. /*   non-commercial purpose, provided that this copyright and authorship    */
  13. /*   notice be included in any copy or derived version of this file.        */
  14. /*                                                                          */
  15. /* DISCLAIMER: This software is offered ``as is'', without any guarantee    */
  16. /*   as to fitness for any particular purpose.  Neither the copyright       */
  17. /*   holder nor the authors or their employers can be held responsible for  */
  18. /*   any damages that may result from its use.                              */
  19. /****************************************************************************/
  20.  
  21. /* Last modified on Sun Aug  2 01:57:14 PDT 1992 by stolfi                  */
  22.  
  23. /* Expands a shrunken wordlist. 
  24.  *
  25.  * Usage: expanddict.c < foo.shrunk > foo
  26.  *
  27.  * This program assumes each line of stdin contains a word, 
  28.  * consisting of a "prefix length", a "suffix" and a newline.
  29.  * The prefix length is an integer in [0..35], encoded as a single
  30.  * byte [0-9A-Z], which represents that many characters from
  31.  * the beginning of the previous word. (In particular, the 
  32.  * prefix lengt of the first word is zero.)  The suffix 
  33.  * is simply the rest of the word.
  34.  *
  35.  * For instance, the word list
  36.  *
  37.  *    baby
  38.  *    back
  39.  *    backs
  40.  *    backstage
  41.  *    backup
  42.  *    bath
  43.  *    cobalt
  44.  *    cobra
  45.  *
  46.  * would be encoded as
  47.  *
  48.  *    0baby
  49.  *    2ck
  50.  *    4s
  51.  *    5tage
  52.  *    4up
  53.  *    2th
  54.  *    0cobalt
  55.  *    3ra
  56.  *
  57.  */
  58.  
  59. #include <stdio.h>
  60.  
  61. #define MAXLENGTH 1000
  62.  
  63. void main()
  64. {
  65.   int c, n, i;
  66.   char w[MAXLENGTH];
  67.   int w_len = 0;
  68.   int bytes_read = 0;
  69.   int bytes_written = 0;
  70.   int words = 0;
  71.   
  72.   /* Loop on words: */
  73.   while ((c = getchar()) != EOF)
  74.     {
  75.       ++bytes_read;
  76.       if ((c >= '0') && (c <= '9'))
  77.         n = c - '0';
  78.       else if ((c >= 'A') && (c <= 'Z'))
  79.         n = c - 'A' + 10;
  80.       else
  81.         { fprintf(stderr, "** bad prefix char at byte %d\n", bytes_read);
  82.           exit(1);
  83.         }
  84.         
  85.       if (n > w_len)
  86.         { fprintf(stderr, "** bad prefix length at byte %d\n", bytes_read);
  87.           exit(1);
  88.         }
  89.       
  90.       /* Copy prefix from previous word: */
  91.       for (i = 0; i < n; i++)
  92.         { putchar(w[i]);
  93.           ++bytes_written;
  94.         };
  95.         
  96.       /* Copy and save rest of word: */
  97.       while ((c = getchar()) != '\n')
  98.         { ++bytes_read;
  99.           putchar(c);
  100.           ++bytes_written;
  101.           if (n >= MAXLENGTH) 
  102.             { fprintf(stderr, "** word too long at byte %d\n", bytes_read);
  103.               exit(1);
  104.             }
  105.           w[n] = c;
  106.           ++n;
  107.         }
  108.       w_len = n;
  109.         
  110.       /* Copy newline: */
  111.       ++bytes_read;
  112.       putchar('\n');
  113.       ++bytes_written;
  114.       ++words;
  115.     }
  116.   
  117.   /* Finalize: */
  118.   fprintf(stderr, "%8d words\n", words);
  119.   fprintf(stderr, "%8d bytes read\n", bytes_read);
  120.   fprintf(stderr, "%8d bytes written\n", bytes_written);
  121.  
  122.   fclose(stdout);
  123.   fclose(stderr);
  124. }
  125.